package org.itboys.mobile.controller.mobile.cookbook;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;

import org.itboys.mobile.dto.cookbook.KitchenCookBookDto;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBook;
import org.itboys.mobile.service.mongo.mobile.cookbook.KitchenMobileCookBookService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017年5月9日
 * 描述：花果山（农厨）_移动端_菜谱_controller
 */
@Controller
@RequestMapping("/kitchen/mobile/cookbook")
public class KitchenMobileCookBookController extends BaseController{
    @Autowired
    private KitchenMobileCookBookService cookBookService;
   /**
     * 菜谱HTML5
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value="/desc",method={ RequestMethod.POST,RequestMethod.GET})
    public  String html5(HttpServletRequest request,
                         Model model,
                         @RequestParam(value="id",required=true) long id){
        KitchenCookBook cookBook = cookBookService.getById(id);
        model.addAttribute("cookBook",cookBook);
        return "/kitchen/cookbook/desc";
    }

    /**
     * 分页查询菜谱
     * @param request
     * @param response
     */
    @RequestMapping(value="/select", method= RequestMethod.POST)
    @ApiOperation(value="获取菜谱。分页")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response,
          //  @ApiParam(required = false, name = "productId", value = "精确查询参数。商品ID") @RequestParam(value = "productId", required = false) Long productId,
            @ApiParam(required = false, name = "name", value = "模糊查询参数。菜谱名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign

    ){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            List<KitchenCookBookDto> result=cookBookService.selectBookCook(request,memberId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/kitchen/mobile/cookbook/select----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 移动端获取-菜谱-详情
     * @param request
     * @param response
     * @param id

     */
    @RequestMapping(value = "/findOne", method = RequestMethod.POST)
    @ApiOperation(value = "获取菜谱详情。code!=0直接提示消息")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "id", value = "菜谱ID") @RequestParam(value = "id", required = true) Long id,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign) {
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            KitchenCookBookDto result = cookBookService.findOne(id,memberId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/kitchen/mobile/cookbook/findOne----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
}
